package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.GoodSpecDto;
import io.geekidea.boot.shop.entity.GoodSpec;
import io.geekidea.boot.shop.mapper.GoodSpecMapper;
import io.geekidea.boot.shop.query.app.AppGoodSpecQuery;
import io.geekidea.boot.shop.query.web.GoodSpecQuery;
import io.geekidea.boot.shop.service.base.api.GoodSpecService;
import io.geekidea.boot.shop.vo.app.AppGoodSpecVo;
import io.geekidea.boot.shop.vo.web.GoodSpecVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class GoodSpecServiceImpl extends ServiceImpl<GoodSpecMapper, GoodSpec> implements GoodSpecService {

    @Autowired
    private GoodSpecMapper goodSpecMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addGoodSpec(GoodSpecDto dto) {
        GoodSpec goodSpec = new GoodSpec();
        BeanUtils.copyProperties(dto, goodSpec);
        return save(goodSpec);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateGoodSpec(GoodSpecDto dto) {
        Integer id = dto.getId();
        GoodSpec goodSpec = getById(id);
        if (goodSpec == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, goodSpec);
        return updateById(goodSpec);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteGoodSpec(Integer id) {
        return removeById(id);
    }

    @Override
    public GoodSpecVo getGoodSpecById(Integer id) {
        return goodSpecMapper.getGoodSpecById(id);
    }

    @Override
    public Paging<GoodSpecVo> getGoodSpecPage(GoodSpecQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<GoodSpecVo> list = goodSpecMapper.getGoodSpecPage(query);
        Paging<GoodSpecVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppGoodSpecVo getAppGoodSpecById(Integer id) {
        return goodSpecMapper.getAppGoodSpecById(id);
    }

    @Override
    public Paging<AppGoodSpecVo> getAppGoodSpecPage(AppGoodSpecQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppGoodSpecVo> list = goodSpecMapper.getAppGoodSpecPage(query);
        Paging<AppGoodSpecVo> paging = new Paging<>(list);
        return paging;
    }

}
